use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        // Create sys_dict_type table
        manager
            .create_table(
                Table::create()
                    .table(SysDictType::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(SysDictType::Id)
                            .big_integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(SysDictType::DictType).string().string_len(100).not_null())
                    .col(ColumnDef::new(SysDictType::DictName).string().string_len(100).not_null())
                    .col(ColumnDef::new(SysDictType::Remark).string().string_len(100))
                    .col(ColumnDef::new(SysDictType::Sort).integer())
                    .col(ColumnDef::new(SysDictType::DictSource).integer().default(0))
                    .col(ColumnDef::new(SysDictType::DictSql).text())
                    .col(ColumnDef::new(SysDictType::Pid).big_integer())
                    .col(ColumnDef::new(SysDictType::HasChild).integer().default(0))
                    .col(ColumnDef::new(SysDictType::TenantId).big_integer())
                    .col(ColumnDef::new(SysDictType::Creator).big_integer())
                    .col(ColumnDef::new(SysDictType::CreatedAt).timestamp_with_time_zone().not_null())
                    .col(ColumnDef::new(SysDictType::Updater).big_integer())
                    .col(ColumnDef::new(SysDictType::UpdatedAt).timestamp_with_time_zone())
                    .col(ColumnDef::new(SysDictType::Version).integer().default(0))
                    .col(ColumnDef::new(SysDictType::Deleted).integer().default(0))
                    .to_owned(),
            )
            .await?;

        // Create sys_dict_data table
        manager
            .create_table(
                Table::create()
                    .table(SysDictData::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(SysDictData::Id)
                            .big_integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(SysDictData::DictTypeId).big_integer().not_null())
                    .col(ColumnDef::new(SysDictData::DictLabel).string().not_null())
                    .col(ColumnDef::new(SysDictData::DictValue).string())
                    .col(ColumnDef::new(SysDictData::LabelClass).string())
                    .col(ColumnDef::new(SysDictData::Remark).string())
                    .col(ColumnDef::new(SysDictData::Sort).integer())
                    .col(ColumnDef::new(SysDictData::TenantId).big_integer())
                    .col(ColumnDef::new(SysDictData::Creator).big_integer())
                    .col(ColumnDef::new(SysDictData::CreatedAt).timestamp_with_time_zone().not_null())
                    .col(ColumnDef::new(SysDictData::Updater).big_integer())
                    .col(ColumnDef::new(SysDictData::UpdatedAt).timestamp_with_time_zone())
                    .col(ColumnDef::new(SysDictData::Version).integer().default(0))
                    .col(ColumnDef::new(SysDictData::Deleted).integer().default(0))
                    .to_owned(),
            )
            .await?;

        // 获取当前时间（用于 create_time 和 update_time）
        let now = Expr::current_timestamp(); //

        // ========== 插入 sys_dict_type 初始数据 ==========
        for data in SYS_DICT_TYPE_DATA.iter() {
            manager
                .exec_stmt(
                    Query::insert().into_table(SysDictType::Table)
                        .columns([
                            SysDictType::Id,
                            SysDictType::DictType,
                            SysDictType::DictName,
                            SysDictType::Remark,
                            SysDictType::Sort,
                            SysDictType::TenantId,
                            SysDictType::Version,
                            SysDictType::Deleted,
                            SysDictType::Creator,
                            SysDictType::CreatedAt,
                            SysDictType::Updater,
                            SysDictType::UpdatedAt,
                        ])
                        .values([
                            data.0.into(),
                            data.1.into(),
                            data.2.into(),
                            data.3.into(), //可为 NULL
                            data.4.into(),    // sort
                            data.5.into(), // tenant_id
                            data.6.into(), // version
                            data.7.into(), // deleted
                            data.8.into(), // creator
                            now.clone().into(),   // create_time
                            data.9.into(), // updater
                            now.clone().into(),   // update_time
                        ])
                        .unwrap().to_owned(),
                )
                .await?;
        }

        // ========== 插入 sys_dict_data 初始数据 ==========
        for data in SYS_DICT_DATA_DATA.iter() {
            manager
                .exec_stmt(
                    Query::insert().into_table(SysDictData::Table)
                        .columns([
                            SysDictData::Id,
                            SysDictData::DictTypeId,
                            SysDictData::DictLabel,
                            SysDictData::DictValue,
                            SysDictData::LabelClass,
                            SysDictData::Remark,
                            SysDictData::Sort,
                            SysDictData::TenantId,
                            SysDictData::Version,
                            SysDictData::Deleted,
                            SysDictData::Creator,
                            SysDictData::CreatedAt,
                            SysDictData::Updater,
                            SysDictData::UpdatedAt,
                        ])
                        .values([
                            data.0.into(),
                            data.1.into(),
                            data.2.into(),
                            data.3.into(), // dict_value
                            data.4.into(), // label_class
                            data.5.into(), // remark
                            data.6.into(),    // sort
                            data.7.into(), // tenant_id
                            data.8.into(), // version
                            data.9.into(), // deleted
                            data.10.into(), // creator
                            now.clone().into(),   // create_time
                            data.11.into(), // updater
                            now.clone().into()// update_time
                        ])
                        .unwrap().to_owned(),
                )
                .await?;
        }

        Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(SysDictData::Table).to_owned())
            .await?;
        
        manager
            .drop_table(Table::drop().table(SysDictType::Table).to_owned())
            .await?;

        Ok(())
    }
}


// ========== 静态数据定义 ==========
const SYS_DICT_TYPE_DATA: &[(i64, &str, &str, Option<&str>, Option<i32>, i64, i32, i32, i64, i64)] = &[
    (1, "post_status", "状态", Some("岗位管理"), Some(0), 10000, 0, 0, 10000, 10000),
    (2, "user_gender", "性别", Some("用户管理"), Some(0), 10000, 0, 0, 10000, 10000),
    (3, "user_status", "状态", Some("用户管理"), Some(0), 10000, 0, 0, 10000, 10000),
    (4, "role_data_scope", "数据范围", Some("角色管理"), Some(0), 10000, 0, 0, 10000, 10000),
    (5, "enable_disable", "状态", Some("功能状态：启用 | 禁用 "), Some(0), 10000, 0, 0, 10000, 10000),
    (6, "success_fail", "状态", Some("操作状态：成功 | 失败"), Some(0), 10000, 0, 0, 10000, 10000),
    (7, "login_operation", "操作信息", Some("登录管理"), Some(0), 10000, 0, 0, 10000, 10000),
    (8, "params_type", "系统参数", Some("参数管理"), Some(0), 10000, 0, 0, 10000, 10000),
    (9, "user_super_admin", "用户是否是超管", Some("用户是否是超管"), Some(0), 10000, 0, 0, 10000, 10000),
    (10, "log_operate_type", "操作类型", Some("操作日志"), Some(0), 10000, 0, 0, 10000, 10000),
    (11, "sms_platform", "短信平台类型", Some("短信管理"), Some(0), 10000, 0, 0, 10000, 10000),
    (12, "mail_platform", "邮件平台类型", Some("邮件管理"), Some(0), 10000, 0, 0, 10000, 10000),
];

const SYS_DICT_DATA_DATA: &[(
    i64,    // id
    i64,    // dict_type_id
    &str,   // dict_label
    Option<&str>, // dict_value
    Option<&str>, // label_class
    Option<&str>, // remark
    Option<i32>,  // sort
    i64,    // tenant_id
    i32,    // version
    i32,    // deleted
    i64,    // creator
    i64,    // updater
)] = &[
    (1, 1, "停用", Some("0"), Some("danger"), Some(""), Some(1), 10000, 0, 0, 10000, 10000),
    (2, 1, "正常", Some("1"), Some("primary"), Some(""), Some(0), 10000, 0, 0, 10000, 10000),
    (3, 2, "男", Some("0"), Some("primary"), Some(""), Some(0), 10000, 0, 0, 10000, 10000),
    (4, 2, "女", Some("1"), Some("success"), Some(""), Some(1), 10000, 0, 0, 10000, 10000),
    (5, 2, "未知", Some("2"), Some("warning"), Some(""), Some(2), 10000, 0, 0, 10000, 10000),
    (6, 3, "正常", Some("1"), Some("primary"), Some(""), Some(0), 10000, 0, 0, 10000, 10000),
    (7, 3, "停用", Some("0"), Some("danger"), Some(""), Some(1), 10000, 0, 0, 10000, 10000),
    (8, 4, "全部数据", Some("0"), Some(""), Some(""), Some(0), 10000, 0, 0, 10000, 10000),
    (9, 4, "本机构及子机构数据", Some("1"), Some(""), Some(""), Some(0), 10000, 0, 0, 10000, 10000),
    (10, 4, "本机构数据", Some("2"), Some(""), Some(""), Some(0), 10000, 0, 0, 10000, 10000),
    (11, 4, "本人数据", Some("3"), Some(""), Some(""), Some(0), 10000, 0, 0, 10000, 10000),
    (12, 4, "自定义数据", Some("4"), Some(""), Some(""), Some(0), 10000, 0, 0, 10000, 10000),
    (13, 5, "禁用", Some("0"), Some("danger"), Some(""), Some(1), 10000, 0, 0, 10000, 10000),
    (14, 5, "启用", Some("1"), Some("primary"), Some(""), Some(0), 10000, 0, 0, 10000, 10000),
    (15, 6, "失败", Some("0"), Some("danger"), Some(""), Some(1), 10000, 0, 0, 10000, 10000),
    (16, 6, "成功", Some("1"), Some("primary"), Some(""), Some(0), 10000, 0, 0, 10000, 10000),
    (17, 7, "登录成功", Some("0"), Some("primary"), Some(""), Some(0), 10000, 0, 0, 10000, 10000),
    (18, 7, "退出成功", Some("1"), Some("warning"), Some(""), Some(1), 10000, 0, 0, 10000, 10000),
    (19, 7, "验证码错误", Some("2"), Some("danger"), Some(""), Some(2), 10000, 0, 0, 10000, 10000),
    (20, 7, "账号密码错误", Some("3"), Some("danger"), Some(""), Some(3), 10000, 0, 0, 10000, 10000),
    (21, 8, "否", Some("0"), Some("primary"), Some(""), Some(1), 10000, 0, 0, 10000, 10000),
    (22, 8, "是", Some("1"), Some("danger"), Some(""), Some(0), 10000, 0, 0, 10000, 10000),
    (23, 9, "是", Some("1"), Some("danger"), Some(""), Some(1), 10000, 0, 0, 10000, 10000),
    (24, 9, "否", Some("0"), Some("primary"), Some(""), Some(0), 10000, 0, 0, 10000, 10000),
    (25, 10, "其它", Some("0"), Some("info"), Some(""), Some(10), 10000, 0, 0, 10000, 10000),
    (26, 10, "查询", Some("1"), Some("primary"), Some(""), Some(0), 10000, 0, 0, 10000, 10000),
    (27, 10, "新增", Some("2"), Some("success"), Some(""), Some(1), 10000, 0, 0, 10000, 10000),
    (28, 10, "修改", Some("3"), Some("warning"), Some(""), Some(2), 10000, 0, 0, 10000, 10000),
    (29, 10, "删除", Some("4"), Some("danger"), Some(""), Some(3), 10000, 0, 0, 10000, 10000),
    (30, 10, "导出", Some("5"), Some("info"), Some(""), Some(4), 10000, 0, 0, 10000, 10000),
    (31, 10, "导入", Some("6"), Some("info"), Some(""), Some(5), 10000, 0, 0, 10000, 10000),
    (32, 11, "阿里云", Some("0"), Some(""), Some(""), Some(0), 10000, 0, 0, 10000, 10000),
    (33, 11, "腾讯云", Some("1"), Some(""), Some(""), Some(1), 10000, 0, 0, 10000, 10000),
    (34, 11, "七牛云", Some("2"), Some(""), Some(""), Some(2), 10000, 0, 0, 10000, 10000),
    (35, 11, "华为云", Some("3"), Some(""), Some(""), Some(3), 10000, 0, 0, 10000, 10000),
    (36, 12, "本地", Some("-1"), Some(""), Some(""), Some(0), 10000, 0, 0, 10000, 10000),
    (37, 12, "阿里云", Some("0"), Some(""), Some(""), Some(1), 10000, 0, 0, 10000, 10000),
];

#[derive(DeriveIden)]
enum SysDictType {
    Table,
    Id,
    DictType,
    DictName,
    Remark,
    Sort,
    DictSource,
    DictSql,
    Pid,
    HasChild,
    TenantId,
    Creator,
    CreatedAt,
    Updater,
    UpdatedAt,
    Version,
    Deleted,
}

#[derive(DeriveIden)]
enum SysDictData {
    Table,
    Id,
    DictTypeId,
    DictLabel,
    DictValue,
    LabelClass,
    Remark,
    Sort,
    TenantId,
    Creator,
    CreatedAt,
    Updater,
    UpdatedAt,
    Version,
    Deleted,
}